package main

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/widget"
)

func main() {
	myApp := app.New()
	myWindow := myApp.NewWindow("树形控件")
	//tree2 := widget.NewTree(nil, nil, nil, nil)

	//tree data
	treeData := map[string][]string{
		"":   {"体育", "美食"},
		"体育": {"羽毛球", "篮球", "跑步"},
		"美食": {"川菜", "粤菜"},
	}
	// 创建tree
	tree := widget.NewTreeWithStrings(treeData)
	// 打开所有分支
	tree.OpenAllBranches()
	// 打开指定分支
	// tree.OpenBranch("美食")

	tree = widget.NewTree(
		func(id widget.TreeNodeID) []widget.TreeNodeID {
			switch id {
			case "":
				return []widget.TreeNodeID{"a", "b", "c", "d", "e", "f"}
			case "a":
				return []widget.TreeNodeID{"a1", "a2"}
			case "b":
				return []widget.TreeNodeID{"b1", "b2"}
			}
			return []string{}
		},
		func(id widget.TreeNodeID) bool {
			return id == "" || id == "a" || id == "b"
		},
		func(branch bool) fyne.CanvasObject {
			if branch {
				return widget.NewLabel("分支模板")
			}
			return widget.NewEntry()
		},
		func(id widget.TreeNodeID, branch bool, o fyne.CanvasObject) {
			//text := id
			text := "节点"
			if branch {
				text += " (分支)"
				o.(*widget.Label).SetText(text)
			} else {
				text += " (叶子)"
				o.(*widget.Entry).SetText(id)
			}
		})

	myWindow.SetContent(tree)
	myWindow.ShowAndRun()
}

/*func refreshTree(tree2 *widget.Tree, root string) {
	// 设置根节点
	tree2.Root = root

	// 获取子节点
	tree2.ChildUIDs = func(uid widget.TreeNodeID) (c []widget.TreeNodeID) {
		if uid == "" {
			c = getFiles(root)
		} else {
			c = getFiles(uid)
		}
		return
	}

	// 创建空节点，节点是个Label组件
	tree2.CreateNode = func(branch bool) (o fyne.CanvasObject) {
		return widget.NewLabel("")
	}

	// 给每个节点的Label组件设置文本
	tree2.UpdateNode = func(uid widget.TreeNodeID, branch bool, node fyne.CanvasObject) {
		l := node.(*widget.Label)
		l.SetText(getFileName(uid))
	}

	// 判断是否是分支节点
	tree2.IsBranch = func(uid widget.TreeNodeID) (ok bool) {
		return isDir(uid)
	}
	//重新渲染组件
	tree2.Refresh()
}
*/
